package com.iaiai.cobra.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iaiai.cobra.common.constant.YesOrNo;
import com.iaiai.cobra.common.util.StringUtil;
import com.iaiai.cobra.common.vo.ResultVo;
import com.iaiai.cobra.core.BaseController;
import com.iaiai.cobra.repository.bean.Dept;
import com.iaiai.cobra.repository.bean.Post;
import com.iaiai.cobra.repository.bean.Role;
import com.iaiai.cobra.repository.bean.RoleMenu;
import com.iaiai.cobra.repository.bean.po.MenuPo;
import com.iaiai.cobra.system.controller.params.RoleMenusParams;
import com.iaiai.cobra.web.params.PageParams;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with RoleController.
 * Package: com.iaiai.cobra.system.controller
 * Author: iaiai
 * Create Time: 2024/7/15 21:40
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @PostMapping("/list.json")
    public ResultVo page(@RequestBody PageParams params) {
        if(params==null)new PageParams();
        if(params.getPage()==null || params.getLimit()==null){
            return fail("参数异常");
        }

        Page<Role> pages = serviceManager.roleService.queryPage(new Page(params.getPage(),params.getLimit()),params.getSearch());
        return success(pages);
    }

    @PostMapping("/add.do")
    public ResultVo add(@RequestBody Role params) {
        if(params==null || StringUtil.isEmpty(params.getName()) || params.getDisabled()==null){
            return fail("参数异常");
        }

        Role maxRole = serviceManager.roleService.queryMax();

        params.setDel(YesOrNo.NO.getValue());
        params.setCreateTime(new Date());
        params.setModifyTime(params.getCreateTime());
        params.setSeq(maxRole==null ? 0 : (maxRole.getSeq().intValue()+1));
        serviceManager.roleService.save(params);

        return success();
    }

    @PostMapping("/detail.json")
    public ResultVo detail(@RequestBody Role params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }
        return success(serviceManager.roleService.getById(params.getId()));
    }

    @PostMapping("/edit.do")
    public ResultVo edit(@RequestBody Role params) {
        if(params==null
                || params.getId()==null
                || StringUtil.isEmpty(params.getName())
                || params.getDisabled()==null){
            return fail("参数异常");
        }

        Role role = serviceManager.roleService.getById(params.getId());
        role.setModifyTime(new Date());
        role.setName(params.getName());
        role.setRemark(params.getRemark());
        role.setDisabled(params.getDisabled());
        serviceManager.roleService.updateById(role);

        return success();
    }

    @PostMapping("/del.do")
    public ResultVo del(@RequestBody Role params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        Role role = serviceManager.roleService.getById(params.getId());
        role.setModifyTime(new Date());
        role.setDel(YesOrNo.YES.getValue());
        serviceManager.roleService.updateById(role);

        serviceManager.roleMenuService.delRole(role.getId());

        return success();
    }

    //查询菜单项，包含子，只查菜单，不包含功能项和子页面，不包含隐藏的不显示的
    @PostMapping("/menu.json")
    public ResultVo menu(@RequestBody Role params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        Map<String,Object> map = new HashMap<>();
        List<MenuPo> menus = serviceManager.menuService.queryAllMenu();
        map.put("menus",menus);

        //查询选中的
        List<RoleMenu> roleMenus = serviceManager.roleMenuService.queryMenus(params.getId());
        List<String> menuIds = roleMenus.stream().map(RoleMenu::getMenuId).map(id->id+"").collect(Collectors.toList());
        map.put("menuIds",menuIds);

        return success(map);
    }

    //保存菜单
    @PostMapping("/menu.do")
    public ResultVo menuEdit(@RequestBody RoleMenusParams params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        serviceManager.roleMenuService.editMenu(params.getId(),params.getMenuIds());
        return success();
    }

}
